home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’93 / Jon’s FKEYs / GrabColor ƒ / GrabColor FKEY.p < prev   
Encoding:
Text File  |  1992-10-04  |  9.8 KB  |  337 lines  |  [TEXT/PJMM]

  1. {    GrabColorFKEY FKEY © 1992 by Jon Wind                                                    }
  2. {    Version 1.0 on 9/26/92                                                                        }
  3.  
  4. {    This FKEY lets you draw a rectangle on the screen and displays it's coordinates.        }
  5.  
  6. {     Thanks to Brad Pettit and his colorfkey for his method of conditional compilation.        }
  7.  
  8. {    To execute this as a program...                                                                }
  9. {        1. change the definition of fkey to false                                                }
  10. {        2. set the project type to application                                                    }
  11. {        3. change the library from drvrruntime.lib to µruntime.lib                            }
  12. {        4. rebuild the project}
  13.  
  14.  
  15. {$setc fkey := true}
  16.  
  17. {$ifc fkey}
  18.  
  19. unit GrabColorFKEY;
  20.  
  21. interface
  22.  
  23.     uses
  24.         Picker;
  25.  
  26.     procedure main;
  27.  
  28. implementation
  29.  
  30. {$elsec}
  31.  
  32.     program GrabColorFKEY;
  33.  
  34.         uses
  35.             Picker;
  36.  
  37. {$endc}
  38.  
  39.         procedure main;
  40.             const
  41.                 vers = 'v1.0';
  42.                 enterKey = 3;
  43.                 lastValIndex = 8;
  44.                 bCommandKey = 48;
  45.                 bShiftKey = 63;
  46.                 bControlKey = 60;
  47.                 bOptionKey = 61;
  48.                 bCapsLockKey = 62;
  49.             type
  50.                 myIntArray = array[0..lastValIndex] of LongInt;
  51.                 myLabelArray = array[0..lastValIndex] of string[2];
  52.                 altCursRec = packed array[1..68] of Byte;
  53.             var
  54.                 p: grafport;
  55.                 sampleRect, theRect, menuRect: Rect;
  56.                 savePort: GrafPtr;
  57.                 CMenuPtr: MCEntryPtr;
  58.                 theEvent: EventRecord;
  59.                 done, usingColor, eraseMBar, updateSample: Boolean;
  60.                 thePoint, oldPoint: Point;
  61.                 h, i, theFont, baseLine, menuHeight: Integer;
  62.                 LabelArray: myLabelArray;
  63.                 oldIntArray, IntArray, WidthArray: myIntArray;
  64.                 gCurs: Cursor;
  65.                 long: LongInt;
  66.                 theStr: str255;
  67.                 equalStr, openCmtStr, closeCmtStr: string[3];
  68.                 oPix, cPix: RGBColor;
  69.                 hColor: HSVColor;
  70.                 cColor: CMYColor;
  71.                 fInfo: FontInfo;
  72.  
  73.  
  74. {•   function GetMBarHeight: Integer;•}
  75. {•    { get current menu bar height •]}
  76. {•    var•}
  77. {•     thePtr: ^Integer;•}
  78. {•   begin•}
  79. {•    thePtr := Pointer($BAA);•}
  80. {•    GetMBarHeight := thePtr^;•}
  81. {•   end;  { of func GetMBarHeight •]}
  82.  
  83.             function GetMBarHeight: INTEGER;
  84.             inline
  85.                 $3EB8, $0BAA;
  86.  
  87.             function IsColor: Boolean;
  88.     { return true if using 16 or more "colors" }
  89.                 var
  90.                     maindevice: GDHandle;
  91.                     theWorld: SysEnvRec;
  92.             begin
  93.                 IsColor := False;
  94.                 if (SysEnvirons(1, theWorld) <> envNotPresent) then    { SysEnvirons call available? }
  95.                     if theWorld.hasColorQD then        { has Color QuickDraw }
  96.                         IsColor := True;
  97.             end;{ of func IsColor }
  98.  
  99.             function myGetGrayRgn: Handle;
  100.     { get current gray region }
  101.                 var
  102.                     thePtr: ^Handle;
  103.             begin
  104.                 thePtr := Pointer($9EE);
  105.                 myGetGrayRgn := thePtr^;
  106.             end;  { of func GetGrayRgn }
  107.  
  108.             function GetKeyDown (index: Integer): Boolean;
  109.     { return the state of the desired key - true if down; false if up }
  110.                 var
  111.                     keys: keymap;
  112.             begin
  113.                 GetKeys(keys);
  114.                 GetKeyDown := bittst(@keys, index);        { look at entry within the key map }
  115.             end;
  116.  
  117.             function aNum2Str (aNum: LongInt): Str255;
  118.     { NumToString procedure available as a function }
  119.                 var
  120.                     NumStr: Str255;
  121.             begin
  122.                 NumToString(aNum, NumStr);
  123.                 aNum2Str := NumStr;
  124.             end;
  125.  
  126.             procedure CompareAndFixCursor;
  127.     { compare current and expected cursors and adjust as needed }
  128.                 var
  129.                     i: Integer;
  130.                     currentCurs: ^Cursor;
  131.             begin
  132.                 currentCurs := Pointer($844);
  133.                 for i := 1 to 68 do            { compare current and expected cursors and adjust as needed }
  134.                     if altCursRec(currentCurs^)[i] <> altCursRec(gCurs)[i] then
  135.                         begin
  136.                             SetCursor(gCurs);
  137.                             leave;
  138.                         end;
  139.             end;  { of proc CompareAndFixCursor }
  140.  
  141.  
  142.  { --------- Main Procedure --------- }
  143.         begin
  144.             GetPort(savePort);            { save current grafport }
  145.  
  146.             usingColor := IsColor;
  147.             if usingcolor then
  148.                 begin
  149.                     OpenCPort(@p);            { open as current port }
  150.                     CMenuPtr := GetMCEntry(0, 0);
  151.                     if CMenuPtr <> nil then
  152.                         begin
  153.                             RGBForeColor(CMenuPtr^.mctRGB1);
  154.                             RGBBackColor(CMenuPtr^.mctRGB4);
  155.                         end;
  156.  
  157.                     LabelArray[0] := 'R:';
  158.                     LabelArray[1] := 'G:';
  159.                     LabelArray[2] := 'B:';
  160.                     LabelArray[3] := 'H:';
  161.                     LabelArray[4] := 'S:';
  162.                     LabelArray[5] := 'V:';
  163.                     LabelArray[6] := 'C:';
  164.                     LabelArray[7] := 'M:';
  165.                     LabelArray[8] := 'Y:';
  166.                     done := False;
  167.                     updateSample := False;
  168.                     eraseMBar := False;
  169.                     for i := 0 to lastValIndex do
  170.                         IntArray[i] := Random;
  171.  
  172.                     oPix.red := Random;
  173.                     oPix.green := Random;
  174.                     oPix.blue := Random;
  175.                     SetPt(oldPoint, maxint, maxint);
  176.                     StuffHex(Pointer(@gCurs), '000E001F001F00FF007E00B801180228044008801100220044004800B0004000000E001F001F00FF007E00F801F803E807C00F801F003E007C007800F0004000000F0001');
  177.  
  178.                     GetFNum('Geneva', theFont);
  179.                     TextFont(theFont);
  180.                     TextSize(9);
  181.                     GetFontInfo(fInfo);
  182.                     menuHeight := GetMBarHeight;
  183. {•     baseLine := Pred(((menuHeight - (fInfo.ascent + fInfo.descent)) div 2) + fInfo.ascent);•}
  184.                     baseLine := Pred(((menuHeight - fInfo.ascent) div 2) + fInfo.ascent);    { NO DESCENDERS USED! }
  185.                     SetRect(menuRect, 1, 0, p.portrect.right, menuHeight - 1);
  186.                     EraseRoundRect(menuRect, 12, 12);
  187.  
  188.                     TextFace([bold]);
  189.                     Moveto(6, baseLine);
  190.                     DrawString('GrabColor FKEY by Jon Wind.');
  191.                     TextFace([]);
  192.                     DrawString(' Click on a color. Press a key to end.');
  193.  
  194.                     SetRect(sampleRect, 0, menuRect.top + 2, 21, menuRect.bottom - 2);
  195.  
  196.                     repeat
  197.                         repeat
  198.                             CompareAndFixCursor;
  199.                         until GetOSEvent(EveryEvent, theEvent);
  200.  
  201.                         case theEvent.what of
  202.                             autokey, keyDown: 
  203.                                 begin
  204.                                     done := True;
  205.                                     if (BitAnd(theEvent.message, CharCodeMask) = enterKey) and eraseMBar then    { copy to scrap if Enter key pressed }
  206.                                         begin
  207.                                             if GetKeyDown(bCapsLockKey) then        { test for caps lock down }
  208.                                                 begin
  209.                                                     equalStr := '=';
  210.                                                     openCmtStr := '/* ';
  211.                                                     closeCmtStr := ' */';
  212.                                                 end
  213.                                             else
  214.                                                 begin
  215.                                                     equalStr := ':=';
  216.                                                     openCmtStr := '{* ';
  217.                                                     closeCmtStr := ' *}';
  218.                                                 end;
  219.                                             theStr := Concat(openCmtStr, 'RGB Model', closeCmtStr);
  220.                                             theStr := Concat(theStr, chr(13), 'red', equalStr, aNum2Str(IntArray[0]), ';green', equalStr, aNum2Str(IntArray[1]), ';blue', equalStr, aNum2Str(IntArray[2]), ';');
  221.                                             theStr := Concat(theStr, chr(13), openCmtStr, 'HSV Model', closeCmtStr);
  222.                                             theStr := Concat(theStr, chr(13), 'hue', equalStr, aNum2Str(IntArray[3]), ';saturation', equalStr, aNum2Str(IntArray[4]), ';value', equalStr, aNum2Str(IntArray[5]), ';');
  223.                                             theStr := Concat(theStr, chr(13), openCmtStr, 'CMY Model', closeCmtStr);
  224.                                             theStr := Concat(theStr, chr(13), 'cyan', equalStr, aNum2Str(IntArray[6]), ';magenta', equalStr, aNum2Str(IntArray[7]), ';yellow', equalStr, aNum2Str(IntArray[8]), ';');
  225.                                             if ZeroScrap = noErr then
  226.                                                 long := PutScrap(Length(theStr), 'TEXT', Pointer(@theStr[1]));
  227.                                         end;
  228.                                 end;
  229.                             mouseDown: 
  230.                                 begin
  231.                                     if not eraseMBar then
  232.                                         begin
  233.                                             EraseRoundRect(menuRect, 12, 12);            { clear menu bar area }
  234.                                             TextFace([bold]);
  235.                                             moveto(10, baseLine);
  236.                                             for i := 0 to lastValIndex do
  237.                                                 begin
  238.                                                     WidthArray[i] := StringWidth(LabelArray[i]);
  239.                                                     DrawString(LabelArray[i]);
  240.                                                     move(40, 0);
  241.  
  242.                                                     if Succ(i) mod 3 = 0 then
  243.                                                         move(30, 0);
  244.  
  245.                                                     oldIntArray[i] := maxint;
  246.                                                 end;
  247.                                             TextFace([]);
  248.                                             MoveTo(menuRect.right - StringWidth(Vers) - 5, baseLine);
  249.                                             DrawString(Vers);
  250.                                             eraseMBar := True;
  251.                                         end;
  252.  
  253.                                     repeat
  254.                                         GetMouse(thePoint);
  255.                                         if not EqualPt(thePoint, oldPoint) then
  256.                                             GetCPixel(thePoint.h, thePoint.v, cPix);
  257.                                         oldPoint := thePoint;
  258.  
  259.                                         if (cPix.red <> oPix.red) | (cPix.green <> oPix.green) | (cPix.blue <> oPix.blue) then
  260.                                             begin
  261.                                                 oPix := cPix;
  262.  
  263.                                                 RGB2HSV(cPix, hColor);
  264.                                                 RGB2CMY(cPix, cColor);
  265.                                                 IntArray[0] := SmallFract2Fix(cPix.red);
  266.                                                 IntArray[1] := SmallFract2Fix(cPix.green);
  267.                                                 IntArray[2] := SmallFract2Fix(cPix.blue);
  268.                                                 IntArray[3] := SmallFract2Fix(hColor.hue);
  269.                                                 IntArray[4] := SmallFract2Fix(hColor.saturation);
  270.                                                 IntArray[5] := SmallFract2Fix(hColor.value);
  271.                                                 IntArray[6] := SmallFract2Fix(cColor.cyan);
  272.                                                 IntArray[7] := SmallFract2Fix(cColor.magenta);
  273.                                                 IntArray[8] := SmallFract2Fix(cColor.yellow);
  274.  
  275.                                                 h := 0;
  276.                                                 for i := 0 to lastValIndex do
  277.                                                     begin
  278.                                                         h := h + WidthArray[i];
  279.                                                         SetRect(theRect, 12 + (40 * i) + h, menuRect.top, (40 * i) + h + 50, menuRect.bottom);
  280.                                                         moveto(theRect.left, baseLine);
  281.                                                         if IntArray[i] <> oldIntArray[i] then
  282.                                                             begin
  283.                                                                 EraseRect(theRect);
  284.                                                                 DrawString(aNum2Str(IntArray[i]));
  285.                                                                 oldIntArray[i] := IntArray[i];
  286.                                                                 updateSample := True;
  287.                                                             end;
  288.  
  289.                                                         if updateSample & (Succ(i) mod 3 = 0) & (i < lastValIndex) then
  290.                                                             begin
  291.                                                                 theRect := SampleRect;
  292.                                                                 OffsetRect(theRect, (40 * Succ(i)) + h + (32 - SampleRect.right), 0);
  293.                                                                 RGBForeColor(cPix);
  294.                                                                 PaintRect(theRect);
  295.                                                                 ForeColor(blackColor);
  296.  
  297.                                                                 if CMenuPtr <> nil then
  298.                                                                     begin
  299.                                                                         RGBForeColor(CMenuPtr^.mctRGB1);
  300.                                                                         RGBBackColor(CMenuPtr^.mctRGB4);
  301.                                                                     end;
  302.  
  303.                                                                 FrameRect(theRect);
  304.                                                             end;
  305.  
  306.                                                         if Succ(i) mod 3 = 0 then
  307.                                                             h := h + 30;
  308.                                                     end;
  309.                                                 updateSample := False;
  310.                                             end    { of (cPix.red <> oPix.red) | (cPix.green <> oPix.green) | (cPix.blue <> oPix.blue) }
  311.                                         else
  312.                                             CompareAndFixCursor;
  313.                                     until not StillDown;
  314.                                 end;
  315.                             otherwise
  316.                         end;
  317.                     until done;
  318.  
  319.                     CloseCPort(@p);
  320.                     InitCursor;
  321.                 end
  322.             else
  323.                 SysBeep(1);
  324.  
  325.             SetPort(savePort);            { restore grafport }
  326.             DrawMenuBar;                    { fix menubar }
  327.         end;    { main }
  328.  
  329.  
  330. {$ifc fkey = false}
  331.  
  332.     begin
  333.         main;
  334.  
  335. {$endc}
  336.  
  337.     end.